package com.habdqn.jspdemo.im.servlet.company;

import com.habdqn.jspdemo.im.enetity.Company;
import com.habdqn.jspdemo.im.service.CompanyService;
import com.habdqn.jspdemo.im.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.UUID;

/**
 * 处理新增公司的请求
 * @author
 */
@WebServlet(name = "AddCompanyServlet",value = "/company/add")
@MultipartConfig
public class AddCompanyServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置请求与响应的编码
        request.setCharacterEncoding("utf-8");

        //接收并封装数据
        String name = request.getParameter("name");
        String city = request.getParameter("city");
        String source = request.getParameter("source");
        String information = request.getParameter("information");
        String strSourceId = request.getParameter("sourceId");
        int sourceId = 4;
        if(strSourceId!=null&&!"".equals(strSourceId)){
            sourceId = Integer.parseInt(strSourceId);
        }

        //接收请求中文件对象
        Part logoPart = request.getPart("logo");

        System.out.println("上传文件类型："+logoPart.getContentType());
        System.out.println("上传文件大小："+logoPart.getSize());
        //获取request的header信息
        Collection<String> headerNames = logoPart.getHeaderNames();
        for (String headerName:headerNames) {
            System.out.println(headerName+"-"+logoPart.getHeader(headerName));
        }
        //获取包含原始文件名的字符串
        String fileNamePath = logoPart.getHeader("content-disposition");
        String fileName = fileNamePath.substring(fileNamePath.indexOf("filename=\"")+10,
                fileNamePath.length()-1);
        System.out.println("原始文件名称"+fileName);
        //生成随机文件名，防止重名
        String newFile = UUID.randomUUID() + fileName.substring(fileName.lastIndexOf("."));

        //将文件写入服务硬盘中
        String logo = getServletContext().getRealPath("/upload/logo")+"/"+newFile;
        logoPart.write(logo);

        Company company = new Company(null,name,city,information,source, sourceId, null, "/upload/logo/"+newFile);
        //调用业务类
        CompanyService companyService = new CompanyServiceImpl();
        boolean flag = companyService.add(company);
        //根据业务类结果，显示不同响应信息
        //因为响应的页面还想输出脚本信息，所以需要设置相应类型
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        if(flag){
            out.print("<script>");
            out.print("alert('新增成功！');");
            out.print("location.href='/company/list';");
            out.print("</script>");
        }else{
            out.print("<script>");
            out.print("alert('新增失败!');");
            out.print("history.back();");
            out.print("</script>");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
